package we.core.handlers;

import java.nio.channels.SocketChannel;

import java.util.HashMap;
import java.util.Map;

import we.common.CCommonHandler;

import we.core.bean.CDataContainer;
import we.core.bean.CRoom;

import we.extension.CSystemEvent;

import org.json.JSONException;
import org.json.JSONObject;


public class CDeleteRoomHandler extends CAbstractCoreHandler {
    public CDeleteRoomHandler() {
    }

    public int handleEvent(SocketChannel channel, JSONObject data) {
        CRoom r = null;
        try {
            r = CDataContainer.instance().getRoom(data.getInt("roomId"));
            CDataContainer.instance().removeRoom(r);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("eventName", CSystemEvent.SystemEventType.DELETE_ROOM);
            params.put("room", r);
            CSystemEvent se = new CSystemEvent(this, params);
            dispatchSystemEvent(se);
            return r.getRoomId();
        } catch (JSONException e) {
            CCommonHandler.writeErrLog(e);
            return -1; // delete room false
            // TODO
        }
    }
}
